ChannelManager.add(channel,guild,cache = true) の動作メモ
channelManager.add(channel,guild,cache = true)
https://github.com/discordjs/discord.js/blob/stable/src/managers/ChannelManager.js#L22
this.cache にある
cache が true
更新
guild が truthy
guildChannelManager.add() を呼び出す
this.cache にない
Channel.create(this.client, data, guild);
cache が true ならば this.cache に登録
code:javascript
add(data, guild, cache = true) {
const existing = this.cache.get(data.id);
if (existing) {
if (existing._patch && cache) existing._patch(data);
if (guild) guild.channels.add(existing);
return existing;
}
const channel = Channel.create(this.client, data, guild);
if (!channel) {
this.client.emit(Events.DEBUG, Failed to find guild, or unknown type for channel ${data.id} ${data.type});
return null;
}
if (cache) this.cache.set(channel.id, channel);
return channel;
}
guildChannelManager.add()
https://github.com/discordjs/discord.js/blob/stable/src/managers/GuildChannelManager.js#L29
あれば無視して、なければ追加
Channel.create()
https://github.com/discordjs/discord.js/blob/stable/src/structures/Channel.js#L99
対象がGuildChannelではない
typeに基づいて適当にインスタンスが作られる
対象がGuildChannel
https://github.com/discordjs/discord.js/blob/stable/src/structures/Channel.js#L111 からが GuildChannel
各種チャンネルが switch で作られる
GuildChannel._patch() がおそらく呼ばれる(おそらくというのは上書きされる可能性が0ではないから)
デフォルトでは各クラスオーバーライドしているが super._patch() している
特に変な副作用はない
最後に guildChannelManager.cache に追加 / 上書きされる
Structures を用いた拡張はここで反映される。